<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="FreeShow" />



<meta property="og:type" content="article">
<meta property="og:title" content="Android之数据存数--SQLite数据库">
<meta property="og:url" content="https://freeshow.github.io/Programming/Android/Android之数据存数-SQLite数据库/index.html">
<meta property="og:site_name" content="FreeShow">
<meta property="og:image" content="http://img.blog.csdn.net/20150914092224095">
<meta property="og:image" content="http://img.blog.csdn.net/20150914104224522">
<meta property="og:image" content="http://img.blog.csdn.net/20150914104507518">
<meta property="og:image" content="http://img.blog.csdn.net/20150914165817870">
<meta property="og:image" content="http://img.blog.csdn.net/20150914183228976">
<meta property="og:image" content="http://img.blog.csdn.net/20150914183604507">
<meta property="og:updated_time" content="2017-03-28T06:57:07.472Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android之数据存数--SQLite数据库">
<meta name="twitter:image" content="http://img.blog.csdn.net/20150914092224095">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="FreeShow" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.ico">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>Android之数据存数--SQLite数据库 | FreeShow</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">FreeShow</a></h1>
        </hgroup>

        
        <p class="header-subtitle">在追求艺术的道路上狂奔</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:877646746@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="http://github.com/freeshow" title="GitHub"></a>
                            
                                <a class="fa 新浪微博" href="http://weibo.com/freeshowself" title="新浪微博"></a>
                            
                                <a class="fa CSDN" href="http://blog.csdn.net/u011026329" title="CSDN"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Android/">Android</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Crawler/">Crawler</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hadoop/">Hadoop</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SIP/">SIP</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Scala/">Scala</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Shell/">Shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spark/">Spark</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tools/">Tools</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WebRTC/">WebRTC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/XMPP/">XMPP</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://github.com/freeshow">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://freeshow.github.io/">FreeShow</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">我是一个喜欢无拘无束、追求自由、热爱分享的小小小码农！</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">FreeShow</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">FreeShow</a></h1>
            </hgroup>
            
            <p class="header-subtitle">在追求艺术的道路上狂奔</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:877646746@qq.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="http://github.com/freeshow" title="GitHub"></a>
                            
                                <a class="fa 新浪微博" target="_blank" href="http://weibo.com/freeshowself" title="新浪微博"></a>
                            
                                <a class="fa CSDN" target="_blank" href="http://blog.csdn.net/u011026329" title="CSDN"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-Programming/Android/Android之数据存数-SQLite数据库" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/Programming/Android/Android之数据存数-SQLite数据库/" class="article-date">
      <time datetime="2016-07-23T15:43:58.000Z" itemprop="datePublished">2016-07-23</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Android之数据存数--SQLite数据库
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/编程语言/">编程语言</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Android/">Android</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><excerpt in="" index="" |="" 首页摘要=""><br><a id="more"></a></excerpt></p>
<the rest="" of="" contents="" |="" 余下全文="">

<p>转载自<a href="http://liangruijun.blog.51cto.com/3061169/663686" target="_blank" rel="external">http://liangruijun.blog.51cto.com/3061169/663686</a><br>《疯狂Android讲义》</p>
<h2 id="一-SQLite的介绍"><a href="#一-SQLite的介绍" class="headerlink" title="一.SQLite的介绍"></a>一.SQLite的介绍</h2><ol>
<li><p>SQLite简介</p>
<p>SQLite是一款轻型的数据库，是遵守ACID的关联式数据库管理系统，它的设计目标是嵌入 式的，而且目前已经在很多嵌入式产品中使用了它，它占用资源非常的低，在嵌入式设备中，可能只需要几百K的内存就够了。它能够支持 Windows/Linux/Unix等等主流的操作系统，同时能够跟很多程序语言相结合，比如Tcl、PHP、Java、C++、.Net等，还有ODBC接口，同样比起 Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲，它的处理速度比他们都快。</p>
<p>Android系统集成了一个轻量级的数据库：SQLite。SQLite只是一个嵌入式的数据库引擎，专门适用于资源有限的设备(如手机、PDA等)上适量数据存取。</p>
<p>虽然SQL支持绝大部分SQL 92语法，也允许开发者使用SQL语句操作数据库中的数据，但SQLite并不像Oracle、MySQL数据库那样需要安装、启动服务器进程，SQLite数据库只是一个文件。</p>
</li>
<li><p>SQLite的特点：</p>
<ul>
<li>轻量级<br>SQLite和C/S模式的数据库软件不同，它是进程内的数据库引擎，因此不存在数据库的客户端和服务器。使用SQLite一般只需要带上它的一个动态  库，就可以享受它的全部功能。而且那个动态库的尺寸也挺小，以版本3.6.11为例，Windows下487KB、Linux下347KB。</li>
</ul>
</li>
</ol>
<ul>
<li><p>不需要”安装”<br>SQLite的核心引擎本身不依赖第三方的软件，使用它也不需要”安装”。有点类似那种绿色软件。</p>
</li>
<li><p>单一文件<br>数据库中所有的信息（比如表、视图等）都包含在一个文件内。这个文件可以自由复制到其它目录或其它机器上。</p>
</li>
<li><p>跨平台/可移植性<br>除了主流操作系统 windows，linux之后，SQLite还支持其它一些不常用的操作系统。</p>
</li>
<li><p>弱类型的字段<br>同一列中的数据可以是不同类型</p>
</li>
<li><p>开源</p>
<ol>
<li>SQLite数据类型</li>
</ol>
</li>
</ul>
<p>一般数据采用的固定的静态数据类型，而SQLite采用的是动态数据类型，会根据存入值自动判断(即它允许把各种类型的数据保存到任意类型字段中)。SQLite具有以下五种常用的数据类型：</p>
<ul>
<li>NULL: 这个值为空值</li>
<li>VARCHAR(n)：长度不固定且其最大长度为 n 的字串，n不能超过 4000。</li>
<li>CHAR(n)：长度固定为n的字串，n不能超过 254。</li>
<li>INTEGER: 值被标识为整数,依据值的大小可以依次被存储为1,2,3,4,5,6,7,8.</li>
<li>REAL(浮点数): 所有值都是浮动的数值,被存储为8字节的IEEE浮动标记序号.</li>
<li>TEXT:(文本) 值为文本字符串,使用数据库编码存储(TUTF-8, UTF-16BE or UTF-16-LE).</li>
<li>BLOB(大二进制对象): 值是BLOB数据块，以输入的数据格式进行存储。如何输入就如何存储,不改  变格式。</li>
<li>DATA ：包含了 年份、月份、日期。</li>
<li>TIME： 包含了 小时、分钟、秒。</li>
</ul>
<hr>
<h2 id="二-SQLiteDatabase的介绍"><a href="#二-SQLiteDatabase的介绍" class="headerlink" title="二.SQLiteDatabase的介绍"></a>二.SQLiteDatabase的介绍</h2><p>Android提供了创建和是用SQLite数据库的API。SQLiteDatabase代表一个数据库对象，提供了操作数据库的一些方法。在Android的SDK目录下有sqlite3工具，我们可以利用它创建数据库、创建表和执行一些SQL语句。下面是SQLiteDatabase的常用方法。</p>
<table>
<thead>
<tr>
<th>方法名称</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory factory)</td>
<td>打开或创建数据库</td>
</tr>
<tr>
<td>insert(String table,String nullColumnHack,ContentValues values)</td>
<td>添加一条记录</td>
</tr>
<tr>
<td>delete(String table,String whereClause,String[] whereArgs)</td>
<td>删除一条记录</td>
</tr>
<tr>
<td>update(String table,ContentValues values,String whereClause,String[] whereArgs)</td>
<td>修改记录</td>
</tr>
<tr>
<td>query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy)</td>
<td>查询一条记录</td>
</tr>
<tr>
<td>execSQL(String sql)</td>
<td>执行一条SQL语句</td>
</tr>
<tr>
<td>beginTransaction()</td>
<td>开始事务</td>
</tr>
<tr>
<td>endTransaction()</td>
<td>结束事务</td>
</tr>
<tr>
<td>close()</td>
<td>关闭事务</td>
</tr>
</tbody>
</table>
<p><strong><em>1.</em></strong> <strong><em>打开或者创建数据库</em></strong></p>
<p>在Android 中以使用SQLiteDatabase的静态方法openOrCreateDatabase(String path,SQLiteDatabae.CursorFactory factory)打开或者创建一个数据库。它会自动去检测是否存在这个数据库，如果存在则打开，不存在则创建一个数据库；创建成功则返回一个SQLiteDatabase对象，否则抛出异常FileNotFoundException。</p>
<p>下面是创建名为“stu.db”数据库的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">SQLiteDatabase db;</div><div class="line">db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+&quot;/stu.db3&quot;,null);</div></pre></td></tr></table></figure>
<p>上面代码没有指定SQLiteDatabase.CursorFactory参数，该参数是一个用于返回Cursor的工厂，如果指定该参数为null,则意味着使用默认的工厂。</p>
<p>上面代码即可返回一个SQLiteDatabase对象，该对象的execSQL()方法可以执行任意的SQL语句。</p>
<p><strong><em>2.</em></strong> <strong><em>创建表</em></strong></p>
<p>创建一张表很简单。首先，编写创建表的SQL语句，然后，调用SQLiteDatabase的execSQL()方法来执行SQL语句便可以创建一张表了。</p>
<p>下面的代码创建了一张学生表，属性列为：id（主键并且自动增加）、sname（学生姓名）、snumber（学号）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">private void createTable(SQLiteDatabase db) &#123;</div><div class="line">       String stu_table = &quot;create table StuTable(id integer primary key autoincrement,sname text,snumber text)&quot;;</div><div class="line">       db.execSQL(stu_table);</div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<p><strong><em>3.插入数据</em></strong></p>
<p>从上面SQLiteDatabase常用的API中可以看出，SQLiteDatabase提供了对数据库操作的的insert(增)、delete(删)、update(改)、query(查)的方法，其实这些方法完全可以通过执行SQL语句来完成，但Android考虑到部分开发者对SQL语法不熟悉，所以提供这种方法帮助开发者以更简单的方式来操作数据库表中的数据。</p>
<p>因此，SQLiteDatabase对数据库的增、删、改、查等操作，有两种方法可已完成。<br>第一种是：使用SQLiteDatabase提供的insert、delete、update、query方法操作数据库。<br>第二种是：使用SQL语句操作SQLite数据库。</p>
<p>第一种方法：<br>SQLiteDatabase的insert(String table,String nullColumnHack,ContentValues values)方法，参数一是表名称，参数二是空列的默认值，参数三是ContentValues类型的一个封装了列名称和列值的Map；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">private void insertDB(SQLiteDatabase db,String name,String number) </div><div class="line">&#123;</div><div class="line">		//实例化常量值</div><div class="line">        ContentValues values = new ContentValues();</div><div class="line">        //插入姓名</div><div class="line">        values.put(&quot;sname&quot;,name);</div><div class="line">        //插入学号</div><div class="line">        values.put(&quot;snumber&quot;,number);</div><div class="line">        //调用insert插入数据</div><div class="line">        db.insert(&quot;stuTable&quot;, null, values);</div><div class="line">&#125;</div><div class="line"></div><div class="line">insertDB(db, &quot;xiaoming&quot;, &quot;00001&quot;);</div><div class="line">insertDB(db, &quot;xiaohong&quot;, &quot;00002&quot;);</div><div class="line">insertDB(db, &quot;xiaoqiang&quot;, &quot;00003&quot;);</div></pre></td></tr></table></figure>
<p>第二种方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">private void insert(SQLiteDatabase db)</div><div class="line">&#123;   </div><div class="line"> </div><div class="line">     //插入数据SQL语句  </div><div class="line">     String stu_sql=&quot;insert into stu_table(sname,snumber) values(&apos;xiaoming&apos;,&apos;00001&apos;)&quot;;  </div><div class="line">   </div><div class="line">    //执行SQL语句  </div><div class="line">     db.execSQL(sql);  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong><em>4.删除数据</em></strong></p>
<p>第一种方法：<br>调用SQLiteDatabase的delete(String table,String whereClause,String[] whereArgs)方法，参数一是表名称，参数二是删除条件，参数三是删除条件值数组；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">private void deleteDB(SQLiteDatabase db)</div><div class="line"> &#123;</div><div class="line">		//删除条件</div><div class="line">        String whereCause = &quot;sname=?&quot;;</div><div class="line">        //删除条件参数</div><div class="line">        String[] whereArgs = &#123;&quot;xiaoming&quot;&#125;;</div><div class="line">        //执行删除</div><div class="line">        db.delete(&quot;stuTable&quot;,whereCause,whereArgs);</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<p>第二种方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">private void deleteDB(SQLiteDatabase db)</div><div class="line"> &#123;  </div><div class="line">   </div><div class="line">   //删除SQL语句  </div><div class="line">   String sql = &quot;delete from stuTable where sname = xiaoming&quot;;  </div><div class="line">   </div><div class="line">   //执行SQL语句  </div><div class="line">   db.execSQL(sql);  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong><em>5.修改数据</em></strong></p>
<p>第一种方法：</p>
<p>调用SQLiteDatabase的update(String table,ContentValues values,String whereClause, String[] whereArgs)方法。参数是表名称，参数是更行列ContentValues类型的键值对（Map），参数是更新条件（where字句），参数是更新条件数组。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">private void updateDB(SQLiteDatabase db)</div><div class="line">&#123;</div><div class="line">    ContentValues values = new ContentValues();</div><div class="line">    values.put(&quot;snumber&quot;,&quot;10010&quot;);</div><div class="line">    String whereCause = &quot;sname=?&quot;;</div><div class="line">    String[] whereArgs = &#123;&quot;xiaohong&quot;&#125;;</div><div class="line">    db.update(&quot;stuTable&quot;,values,whereCause,whereArgs);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>第二种方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">private void updateDB(SQLiteDatabase db)</div><div class="line">&#123;  </div><div class="line">   </div><div class="line">    //修改SQL语句  </div><div class="line">    String sql = &quot;update stuTable set snumber = 10010 where sname = xiaohong&quot;;  </div><div class="line">   </div><div class="line">    //执行SQL  </div><div class="line">    db.execSQL(sql);   </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong><em>6.查询数据</em></strong></p>
<p>在Android中查询数据是通过Cursor类来实现的，当我们使用SQLiteDatabase.query()方法时，会得到一个Cursor对象，Cursor指向的就是每一条数据。它提供了很多有关查询的方法，具体方法如下：<br>public Cursor query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit);<br>各个参数的意义说明：<br>①table:表名称<br>②columns:列名称数组<br>③selection:条件字句，相当于where<br>④selectionArgs:条件字句，参数数组<br>⑤groupBy:分组列<br>⑥having:分组条件<br>⑦orderBy:排序列<br>⑧limit:分页查询限制<br>⑨Cursor:返回值，相当于结果集ResultSet</p>
<p>Cursor是一个游标接口，提供了遍历查询结果的方法，如移动指针方法move()，获得列值方法getString()等.<br>Cursor游标常用方法:<br>| 方法名称 | 方法描述 |<br>| ————- |:————-:|<br>| getCount() | 获得总的数据项数 |<br>| isFirst() | 判断是否第一条记录 |<br>| isLast() | 判断是否最后一条记录 |<br>|moveToFirst()|移动到第一条记录|<br>|moveToLast()|移动到最后一条记录|<br>|moveToPrevious()|移动到上一条记录|<br>|moveToNext()|移动到下一条记录|<br>|move(int offset)|将记录指针移动指定的行数，offset为正就向下移动，offset为负就向上移动。|<br>|getColumnIndexOrThrow(String columnName)|根据列名称获得列索引|<br>|getInt(int columnIndex)|获得指定列索引的int类型值|<br>|getString(int columnIndex)|获得指定列索引的String类型值|</p>
<p>第一种方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">private void queryDB(SQLiteDatabase db)</div><div class="line">    &#123;</div><div class="line">        Cursor cursor = db.query(&quot;stuTable&quot;,null,null,null,null,null,null);</div><div class="line">        if (cursor.moveToFirst())</div><div class="line">        &#123;</div><div class="line">            for (int i = 0;i &lt; cursor.getCount();i++)</div><div class="line">            &#123;</div><div class="line">                cursor.move(i);</div><div class="line">                int id = cursor.getInt(0);</div><div class="line">                String name = cursor.getString(1);</div><div class="line">                String number = cursor.getString(2);</div><div class="line">                System.out.println(id+&quot;:&quot;+name+&quot;:&quot;+number);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>第二种方法：<br>SQLiteDatabase的execSQL()方法可执行任意的SQL语句，包括带占位符的SQL语句。但由于该方法没有返回值，因此一般用于执行DDL语句或DML语句；如果需要执行查询语句，则可调用SQLiteDatabase的rawQuery(String sql,String[] selectionArgs)方法。</p>
<p><strong><em>7.删除指定表</em></strong></p>
<p>编写插入数据的SQL语句，直接调用SQLiteDatabase的execSQL()方法来执行<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">private void dropTable(SQLiteDatabase db) </div><div class="line">&#123;</div><div class="line">        String sql = &quot;DROP TABLE stuTable&quot;;</div><div class="line">        db.execSQL(sql);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong><em>提示：</em></strong>虽然Android提供了这些所谓的“便捷”方法来操作SQLite数据库，但在笔者看来这些方法纯属“鸡肋”，对于一个程序员而言，SQL语法可以说是基本功中的基本功—你见过不会1+1=2的数学工作者吗？</p>
<p><strong><em>8.事务</em></strong><br>SQLiteDatabase中包含如下两个方法来控制事务。</p>
<ul>
<li>beginTransaction():开始事务</li>
<li>endTransaction():结束事务</li>
</ul>
<p>除此之外，SQLiteDatabase还提供了如下方法来判断当前上下文是否处于事务环境中。</p>
<ul>
<li>inTransaction():如果当前上下文处于事务中，则返回true;否则返回false。</li>
</ul>
<p>当程序执行endTransaction()方法时将会结束事务——到底是提交事务，还是回滚事务呢？这取决于SQLiteDatabase是否调用了setTransactionSuccessful()方法来设置事务标志，如果程序在事务执行中调用该方法设置了事务成功则提交事务；否则程序将会回滚事务。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">//开始事务</div><div class="line">db.beginTrainsaction();</div><div class="line">try</div><div class="line">&#123;</div><div class="line">	//指定DML语句</div><div class="line">	......</div><div class="line">	//调用该方法设置事务成功；否则endTransaction()方法将回滚事务。</div><div class="line">	db.setTransactionSuccessful();</div><div class="line">&#125;</div><div class="line">finally</div><div class="line">&#123;</div><div class="line">	//由事务的标志决定是提交事务还是回滚事务</div><div class="line">	db.endTransaction();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Demo:<br>该程序提供两个文本框，用户可以在这两个文本框中输入内容，当用户点击“插入”按钮时这两个文本框的内容将会被插入数据库。</p>
<p>效果：<br><img src="http://img.blog.csdn.net/20150914092224095" alt="这里写图片描述"></p>
<p>代码：<br><strong>main.xml</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</div><div class="line">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</div><div class="line">	android:orientation=&quot;vertical&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot;</div><div class="line">	android:layout_height=&quot;match_parent&quot;&gt;</div><div class="line">&lt;EditText </div><div class="line">	android:id=&quot;@+id/title&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot; </div><div class="line">	android:layout_height=&quot;wrap_content&quot; </div><div class="line">	/&gt;</div><div class="line">&lt;EditText  </div><div class="line">	android:id=&quot;@+id/content&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot; </div><div class="line">	android:layout_height=&quot;wrap_content&quot; </div><div class="line">	android:lines=&quot;2&quot;</div><div class="line">	/&gt;	</div><div class="line">&lt;Button  </div><div class="line">	android:id=&quot;@+id/ok&quot;</div><div class="line">	android:layout_width=&quot;wrap_content&quot; </div><div class="line">	android:layout_height=&quot;wrap_content&quot; </div><div class="line">	android:text=&quot;@string/insert&quot;</div><div class="line">	/&gt;</div><div class="line">&lt;ListView  </div><div class="line">	android:id=&quot;@+id/show&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot; </div><div class="line">	android:layout_height=&quot;match_parent&quot; </div><div class="line">	/&gt;			</div><div class="line">&lt;/LinearLayout&gt;</div></pre></td></tr></table></figure>
<p><strong>line.xml</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</div><div class="line">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</div><div class="line">	android:orientation=&quot;horizontal&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot;</div><div class="line">	android:layout_height=&quot;match_parent&quot;&gt;</div><div class="line">&lt;EditText </div><div class="line">	android:id=&quot;@+id/my_title&quot;</div><div class="line">	android:layout_width=&quot;wrap_content&quot; </div><div class="line">	android:layout_height=&quot;wrap_content&quot; </div><div class="line">	android:width=&quot;120dp&quot;</div><div class="line">	/&gt;</div><div class="line">&lt;EditText  </div><div class="line">	android:id=&quot;@+id/my_content&quot;</div><div class="line">	android:layout_width=&quot;match_parent&quot; </div><div class="line">	android:layout_height=&quot;wrap_content&quot; </div><div class="line">	/&gt;	</div><div class="line">&lt;/LinearLayout&gt;</div></pre></td></tr></table></figure>
<p>MainActivity.java</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div></pre></td><td class="code"><pre><div class="line">package org.crazyit.db;</div><div class="line"></div><div class="line">import android.app.Activity;</div><div class="line">import android.database.Cursor;</div><div class="line">import android.database.sqlite.SQLiteDatabase;</div><div class="line">import android.database.sqlite.SQLiteException;</div><div class="line">import android.os.Bundle;</div><div class="line">import android.view.View;</div><div class="line">import android.view.View.OnClickListener;</div><div class="line">import android.widget.Button;</div><div class="line">import android.widget.CursorAdapter;</div><div class="line">import android.widget.EditText;</div><div class="line">import android.widget.ListView;</div><div class="line">import android.widget.SimpleCursorAdapter;</div><div class="line"></div><div class="line"></div><div class="line">public class MainActivity extends Activity</div><div class="line">&#123;</div><div class="line">	SQLiteDatabase db;</div><div class="line">	Button bn = null;</div><div class="line">	ListView listView;</div><div class="line">	@Override</div><div class="line">	public void onCreate(Bundle savedInstanceState)</div><div class="line">	&#123;</div><div class="line">		super.onCreate(savedInstanceState);</div><div class="line">		setContentView(R.layout.main);</div><div class="line">		// 创建或打开数据库（此处需要使用绝对路径）</div><div class="line">		db = SQLiteDatabase.openOrCreateDatabase(</div><div class="line">				this.getFilesDir().toString()</div><div class="line">						+ &quot;/my.db3&quot;, null); // ①</div><div class="line">		listView = (ListView) findViewById(R.id.show);</div><div class="line">		bn = (Button) findViewById(R.id.ok);</div><div class="line">		bn.setOnClickListener(new OnClickListener()</div><div class="line">		&#123;</div><div class="line">			@Override</div><div class="line">			public void onClick(View source)</div><div class="line">			&#123;</div><div class="line">				// 获取用户输入</div><div class="line">				String title = ((EditText) findViewById(</div><div class="line">						R.id.title)).getText().toString();</div><div class="line">				String content = ((EditText) findViewById(R.id.content))</div><div class="line">						.getText().toString();</div><div class="line">				try</div><div class="line">				&#123;</div><div class="line">					insertData(db, title, content);</div><div class="line">					Cursor cursor = db.rawQuery(&quot;select * from news_inf&quot;</div><div class="line">							, null);</div><div class="line">					inflateList(cursor);</div><div class="line">				&#125;</div><div class="line">				catch (SQLiteException se)</div><div class="line">				&#123;</div><div class="line">					//如果数据库中不包含数据表，则创建数据库表。</div><div class="line">					// 执行DDL创建数据表</div><div class="line">					db.execSQL(&quot;create table news_inf(_id integer&quot;</div><div class="line">							+ &quot; primary key autoincrement,&quot;</div><div class="line">							+ &quot; news_title varchar(50),&quot;</div><div class="line">							+ &quot; news_content varchar(255))&quot;);</div><div class="line">					// 执行insert语句插入数据</div><div class="line">					insertData(db, title, content);</div><div class="line">					// 执行查询</div><div class="line">					Cursor cursor = db.rawQuery(&quot;select * from news_inf&quot;</div><div class="line">							, null);</div><div class="line">					//使用ListView将查询结果显示出来。</div><div class="line">					inflateList(cursor);</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">		&#125;);</div><div class="line">	&#125;</div><div class="line">	private void insertData(SQLiteDatabase db</div><div class="line">			, String title, String content)  // ②</div><div class="line">	&#123;</div><div class="line">		// 执行插入语句</div><div class="line">		db.execSQL(&quot;insert into news_inf values(null , ? , ?)&quot;</div><div class="line">				, new String[] &#123;title, content &#125;);</div><div class="line">	&#125;</div><div class="line">	private void inflateList(Cursor cursor)</div><div class="line">	&#123;</div><div class="line">		// 填充SimpleCursorAdapter</div><div class="line">		SimpleCursorAdapter adapter = new SimpleCursorAdapter(</div><div class="line">				MainActivity.this,</div><div class="line">				R.layout.line, cursor,</div><div class="line">				new String[] &#123; &quot;news_title&quot;, &quot;news_content&quot; &#125;</div><div class="line">				, new int[] &#123;R.id.my_title, R.id.my_content &#125;,</div><div class="line">				// ③</div><div class="line">				//将cousor封装成SimpleCursorAdapter</div><div class="line">				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);  </div><div class="line">		// 显示数据</div><div class="line">		listView.setAdapter(adapter);</div><div class="line">	&#125;</div><div class="line">	@Override</div><div class="line">	public void onDestroy()</div><div class="line">	&#123;</div><div class="line">		super.onDestroy();</div><div class="line">		// 退出程序时关闭SQLiteDatabase</div><div class="line">		if (db != null &amp;&amp; db.isOpen())</div><div class="line">		&#123;</div><div class="line">			db.close();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>程序中③号粗体字代码用于将Cursor封装成SimpleCursorAdapter，这个SimpleCursorAdapter实现了Adapter接口，因此可以作为ListView或GridView的内容适配器。</p>
<p>SimpleCursorAdapter的构造器参数与SimpleAdapter的构造器参数大致相同，区别是SimpleAdapter负责封装集合元素的为Map的List，而SimpleCursorAdapter负责封装Cursor——如果我们把Cursor里的结果集当成List集合，Cursor里的每一行当成Map处理(以数据列的列名为key,数据列的值为value),那么SimpleCursorAdapter与SimpleAdapter就统一起来了。</p>
<p>运行上面程序即可生成数据库文件，如下图：<br><img src="http://img.blog.csdn.net/20150914104224522" alt="这里写图片描述"></p>
<p>将my.db3数据库文件导出，用SQLite管理工具打开即可。<br><a href="http://www.oschina.net/news/43608/5-popular-and-free-sqlite-management-tools" target="_blank" rel="external">5 个免费的受欢迎的 SQLite 管理工具</a></p>
<p>打开my.db3数据库，如下图所示：<br><img src="http://img.blog.csdn.net/20150914104507518" alt="这里写图片描述"></p>
<hr>
<h2 id="三-SQLiteOpenHelper类"><a href="#三-SQLiteOpenHelper类" class="headerlink" title="三.SQLiteOpenHelper类"></a>三.SQLiteOpenHelper类</h2><p>在上面的Demo程序中，我们为了判断底层数据库是否包含news_inf数据表，采用的处理方法十分繁琐：程序先尝试向news_inf数据表中插入记录，如果程序抛出异常，在异常捕获的catch块中创建news_inf数据表，然后在插入记录，那么是否有一种更优雅的方式来处理这种问题呢？有Android提供了SQLiteOpenHelper类来处理这种问题。</p>
<p>在实际项目中很少使用SQLiteDatabase的方法来打开数据库，通常会继承SQLiteOpenHelper开发子类，并通过该子类的getReadableDatabase()、getWritableDatabase()方法打开数据库。</p>
<p>SQLiteOpenHelper是Android提供的一个管理数据库的工具类，可用于管理数据库的创建和版本更新。一般用法是创建SQLiteOpenHelper的子类，并扩展它的onCreate(SQLiteDatabase db)、onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)方法。</p>
<p>SQLiteOpenHelper包含如下常用方法。</p>
<ul>
<li>synchronized SQLiteDatabase getReadableDatabase():以读写的方式打开数据库对应的SQLiteDatabase对象。</li>
<li>synchronized SQLiteDatabase getWritableDatabase():以写的方式打开数据库对应的SQLiteDatabase对象。</li>
<li>abstract void onCreate(SQLiteDatabase db):用于初次使用软件时生成数据库表。当调用SQLiteOpenHelper的getReadableDatabase()或getWritableDatabase()方法用于获取操作数据库的SQLiteDatabase实例时，如果数据库不存在，Android系统会自动生成一个数据库，接着调用onCreate()方法，onCreate()方法在初次生成数据库时才会调用。重写onCreate()方法时，可以生成数据库表结构，以添加应用使用到的一些初始化数据。</li>
<li>abstract void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion):用于升级软件时更新数据库表结构，此方法在数据库的版本发生变化时会调用，该方法调用时oldVersion代表之前的版本号，newVersion代表数据库当前的版本号。那么在哪里指定数据库的版本号呢？当程序创建SQLiteOpenHelper对象时，必须执行一个version参数，该参数就决定了所使用的版本号——也就是说，数据库的版本号是由程序员控制的。只要某次创建SQLiteOpenHelper时指定的数据库的版本号高于之前指定的版本号，系统会自动触发onUpgrade()方法，程序就可以在onUpgrade()方法里面根据原版本号和目标版本号进行判断，即可根据版本号进行必须的表结构更新。</li>
<li>synchronized void close():关闭所有打开的SQLiteDatabase对象。</li>
</ul>
<p>一旦获得了SQLiteOpenHelper对象之后，程序无需使用SQLiteDatabase的静态方法创建SQLiteDatabase实例，而且可以使用getWritableDatabase()或getReadableDatabase()方法来获取一个用于操作数据库的SQLiteDatabase实例。</p>
<p><strong><em>Demo:英文生词本</em></strong><br>允许用户将自己不熟悉的单词添加到系统数据库中，当用户需要查询某个单词或解释时，只要在程序中输入相应的关键词，程序中相应的条目就会显示出来。</p>
<p>效果：<br><img src="http://img.blog.csdn.net/20150914165817870" alt="这里写图片描述"></p>
<p>代码：<br>界面布局文件我就不写了。<br><strong><em>MyDatabaseHelper.java</em></strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">package com.example.sqliteopenhelper;</div><div class="line"></div><div class="line">import android.content.Context;</div><div class="line">import android.database.sqlite.SQLiteDatabase;</div><div class="line">import android.database.sqlite.SQLiteOpenHelper;</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by songxitang on 2015/9/14.</div><div class="line"> */</div><div class="line">public class MyDatabaseHelper extends SQLiteOpenHelper </div><div class="line">&#123;</div><div class="line">    final String CREATE_TABLE_SQL = &quot;create table dict(_id integer primary &quot;+</div><div class="line">            &quot;key autoincrement,word,detail)&quot;;</div><div class="line"></div><div class="line">    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) </div><div class="line">    &#123;</div><div class="line">        super(context, name, factory, version);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void onCreate(SQLiteDatabase db)</div><div class="line">    &#123;</div><div class="line">	    //第一次使用数据库时自动建表。</div><div class="line">        db.execSQL(CREATE_TABLE_SQL);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) </div><div class="line">    &#123;</div><div class="line">        System.out.println(&quot;---onUpgrade Called---: &quot;+oldVersion+&quot;---&gt;&quot;+newVersion);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong><em>MainActivity.java</em></strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div></pre></td><td class="code"><pre><div class="line">package com.example.sqliteopenhelper;</div><div class="line"></div><div class="line">import android.app.Activity;</div><div class="line">import android.content.Intent;</div><div class="line">import android.database.Cursor;</div><div class="line">import android.database.sqlite.SQLiteDatabase;</div><div class="line">import android.os.Bundle;</div><div class="line">import android.view.Menu;</div><div class="line">import android.view.MenuItem;</div><div class="line">import android.view.View;</div><div class="line">import android.widget.Button;</div><div class="line">import android.widget.EditText;</div><div class="line">import android.widget.Toast;</div><div class="line"></div><div class="line">import java.io.Serializable;</div><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.HashMap;</div><div class="line">import java.util.Map;</div><div class="line"></div><div class="line">public class MainActivity extends Activity</div><div class="line">&#123;</div><div class="line">    MyDatabaseHelper dbHelper;</div><div class="line">    Button insert;</div><div class="line">    Button search;</div><div class="line">    EditText word;</div><div class="line">    EditText detail;</div><div class="line">    EditText key;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    protected void onCreate(Bundle savedInstanceState)</div><div class="line">    &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_main);</div><div class="line">		</div><div class="line">		//创建MyDatabaseHelper对象，指定数据库的版本为1，此处使用相对路径即可</div><div class="line">		//数据库文件会自动保存在程序的数据文件夹的databases目录下。</div><div class="line">        dbHelper = new MyDatabaseHelper(this,&quot;myDict.db3&quot;,null,1);</div><div class="line">        insert = (Button) findViewById(R.id.insert);</div><div class="line">        search = (Button) findViewById(R.id.search);</div><div class="line">        word = (EditText) findViewById(R.id.word);</div><div class="line">        detail = (EditText) findViewById(R.id.detail);</div><div class="line">        key = (EditText) findViewById(R.id.key);</div><div class="line"></div><div class="line">        insert.setOnClickListener(new View.OnClickListener()</div><div class="line">        &#123;</div><div class="line">            @Override</div><div class="line">            public void onClick(View v)</div><div class="line">            &#123;</div><div class="line">	            //获取用户输入</div><div class="line">                String sWord = word.getText().toString();</div><div class="line">                String sDetail = detail.getText().toString();</div><div class="line">//插入生词记录                insertData(dbHelper.getReadableDatabase(),sWord,sDetail);</div><div class="line">//显示提示信息              Toast.makeText(getApplicationContext(),&quot;Insert Word Successful&quot;,</div><div class="line">                        Toast.LENGTH_SHORT).show();</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line"></div><div class="line">        search.setOnClickListener(new View.OnClickListener()</div><div class="line">        &#123;</div><div class="line">            @Override</div><div class="line">            public void onClick(View v)</div><div class="line">            &#123;</div><div class="line">	            //获取用户输入</div><div class="line">                String sKey = key.getText().toString();</div><div class="line">                //执行查询</div><div class="line">                Cursor cursor = dbHelper.getReadableDatabase().rawQuery(&quot;&quot; +</div><div class="line">                                &quot;select * from dict where word like ? or detail like ?&quot;,</div><div class="line">                        new String[]&#123;&quot;%&quot; + sKey + &quot;%&quot;, &quot;%&quot; + key + &quot;%&quot;&#125;);</div><div class="line">                //创建一个Bundle对象</div><div class="line">                Bundle data = new Bundle();</div><div class="line">                data.putSerializable(&quot;data&quot;,converCursorToList(cursor));</div><div class="line">                //创建一个intent</div><div class="line">                Intent intent = new Intent(MainActivity.this,ResultActivity.class);</div><div class="line">                intent.putExtras(data);</div><div class="line">                //启动Activity</div><div class="line">                startActivity(intent);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    private ArrayList&lt;Map&lt;String, String&gt;&gt; converCursorToList(Cursor cursor)</div><div class="line">    &#123;</div><div class="line">        ArrayList&lt;Map&lt;String,String&gt;&gt; result = new ArrayList&lt;&gt;();</div><div class="line">        //遍历Cursor结果集</div><div class="line">        while (cursor.moveToNext())</div><div class="line">        &#123;</div><div class="line">	        //将结果集中的数据存入ArrayList中</div><div class="line">            Map&lt;String,String&gt; map = new HashMap&lt;&gt;();</div><div class="line">            //取出查询记录中第2列和第3列的值</div><div class="line">            map.put(&quot;word&quot;,cursor.getString(1));</div><div class="line">            map.put(&quot;detail&quot;,cursor.getString(2));</div><div class="line">            result.add(map);</div><div class="line">        &#125;</div><div class="line">        return result;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    private void insertData(SQLiteDatabase db, String word, String detail)</div><div class="line">    &#123;</div><div class="line">	    //执行插入语句</div><div class="line">        db.execSQL(&quot;insert into dict values(null,?,?)&quot;</div><div class="line">                ,new String[]&#123;word,detail&#125;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    protected void onDestroy()</div><div class="line">    &#123;</div><div class="line">        super.onDestroy();</div><div class="line">        //退出程序是关闭数据库</div><div class="line">        if (dbHelper != null)</div><div class="line">        &#123;</div><div class="line">            dbHelper.close();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong><em>ResultActivity.java</em></strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">package com.example.sqliteopenhelper;</div><div class="line"></div><div class="line">import android.app.Activity;</div><div class="line">import android.content.Intent;</div><div class="line">import android.os.Bundle;</div><div class="line">import android.view.Menu;</div><div class="line">import android.view.MenuItem;</div><div class="line">import android.widget.ListView;</div><div class="line">import android.widget.SimpleAdapter;</div><div class="line"></div><div class="line">import java.util.List;</div><div class="line">import java.util.Map;</div><div class="line"></div><div class="line">public class ResultActivity extends Activity</div><div class="line">&#123;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    protected void onCreate(Bundle savedInstanceState)</div><div class="line">    &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_result);</div><div class="line"></div><div class="line">        ListView listView = (ListView) findViewById(R.id.show);</div><div class="line">        Intent intent = getIntent();</div><div class="line">        //获取该intent所携带的数据</div><div class="line">        Bundle data = intent.getExtras();</div><div class="line">        //从Bundle数据包中取出数据</div><div class="line">        List&lt;Map&lt;String,String&gt;&gt; list = (List&lt;Map&lt;String, String&gt;&gt;) data.getSerializable(&quot;data&quot;);</div><div class="line">        //将List封装成SimpleAdapter</div><div class="line">        SimpleAdapter adapter = new SimpleAdapter(ResultActivity.this,list,R.layout.line,</div><div class="line">                new String[]&#123;&quot;word&quot;,&quot;detail&quot;&#125;,new int[]&#123;R.id.word,R.id.detail&#125;);</div><div class="line">        //填充ListView</div><div class="line">        listView.setAdapter(adapter);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>上面的ResultActivity只是一个普通的Activity，但是我们在AndroidManifest.xml文件中将该Activity设为对话框风格的Activity，这样就可让应用程序已对话框来显示查询结构。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;activity</div><div class="line">        android:name=&quot;.ResultActivity&quot;</div><div class="line">        android:label=&quot;@string/title_activity_result&quot;</div><div class="line">	//以对话框模式来显示Activity。</div><div class="line">        android:theme=&quot;@android:style/Theme.Material.Dialog&quot;&gt;</div><div class="line">&lt;/activity&gt;</div></pre></td></tr></table></figure>
<p>默认生成的数据库文件存放位置：<br><img src="http://img.blog.csdn.net/20150914183228976" alt="这里写图片描述"></p>
<p>数据库文件内容：<br><img src="http://img.blog.csdn.net/20150914183604507" alt="这里写图片描述"></p>
</the>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/Programming/Android/Android之数据存数-SQLite数据库/">Android之数据存数--SQLite数据库</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">FreeShow</a></p>
        <p><span>发布时间:</span>2016-07-23, 23:43:58</p>
        <p><span>最后更新:</span>2017-03-28, 14:57:07</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/Programming/Android/Android之数据存数-SQLite数据库/" title="Android之数据存数--SQLite数据库">https://freeshow.github.io/Programming/Android/Android之数据存数-SQLite数据库/</a>
            <span class="copy-path" data-clipboard-text="原文: https://freeshow.github.io/Programming/Android/Android之数据存数-SQLite数据库/　　作者: FreeShow" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/Programming/Android/Android之话筒、听筒、扬声器/">
                    Android之话筒、听筒、扬声器
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/Programming/Android/Android之AdapterView及子类/">
                    Android之AdapterView及子类
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一-SQLite的介绍"><span class="toc-number">1.</span> <span class="toc-text">一.SQLite的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二-SQLiteDatabase的介绍"><span class="toc-number">2.</span> <span class="toc-text">二.SQLiteDatabase的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三-SQLiteOpenHelper类"><span class="toc-number">3.</span> <span class="toc-text">三.SQLiteOpenHelper类</span></a></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"Android之数据存数--SQLite数据库　| FreeShow　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
      <div class="duoshuo" id="comments">
    <div id="comment-box" ></div>
    <div class="ds-thread" id="ds-thread" data-thread-key="Programming/Android/Android之数据存数-SQLite数据库/" data-title="Android之数据存数--SQLite数据库" data-url="https://freeshow.github.io/Programming/Android/Android之数据存数-SQLite数据库/"></div>
    <script>
        var duoshuoQuery = {short_name:"freeshowgithub"};
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
            s.async = true; s.charset = 'UTF-8';
            (d.head || d.body).appendChild(s);
        }

        
    </script>
    
    <script> loadComment(); </script>

</div>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/Programming/Android/Android之话筒、听筒、扬声器/" title="上一篇: Android之话筒、听筒、扬声器">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/Programming/Android/Android之AdapterView及子类/" title="下一篇: Android之AdapterView及子类">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Hadoop分布式集群安装/">Hadoop分布式集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hive/Hive应用实例：WordCount/">Hive应用实例：WordCount</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hive/Hive安装/">Hive安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Windows下使用eclipse插件运行自己的MapReduce程序/">Windows下使用Eclipse插件运行自己的MapReduce程序</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之去重计数类应用/">MapReduce之去重计数类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之简单排序类应用/">MapReduce之简单排序类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之连接操作类应用/">MapReduce之连接操作类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之计数类应用/">MapReduce之计数类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之二次排序类应用/">MapReduce之二次排序类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/MapReduce之倒排索引类应用/">MapReduce之倒排索引类应用</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/HDFS Java API/">HDFS Java API</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Kafka/Kafka集群安装/">Kafka集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Hadoop/Windows下使用eclipse编译打包运行自己的MapReduce程序 Hadoop2.6.0/">Windows下使用eclipse编译打包运行自己的MapReduce程序 Hadoop2.6.0</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/ZooKeeper/ZooKeeper集群安装/">ZooKeeper集群安装</a></li><li class="post-list-item"><a class="post-list-link" href="/BigData/Spark/Spark安装和集群部署/">Spark安装和集群部署</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/GitHub Pages + Hexo搭建个人博客/">GitHub Pages + Hexo搭建个人博客</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：登录豆瓣并修改签名/">Python爬虫实例：登录豆瓣并修改签名</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：用requests重构豆瓣热播电影爬虫/">Python爬虫实例：用requests重构豆瓣热播电影爬虫</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：豆瓣热播电影/">Python爬虫实例：豆瓣热播电影（urllib+urllib2）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之requests介绍/">Python爬虫之requests介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之urllib2介绍/">Python爬虫之urllib2介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫之urllib介绍/">Python爬虫之urllib介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Cookie介绍/">Cookie介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：从百度图片下载壁纸/">Python爬虫实例：从百度图片下载壁纸</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python线程/">Python线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/XPath与多线程爬虫/">XPath与多线程爬虫</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python之正则表达式/">Python之正则表达式</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫实例：唐诗三百首/">Python爬虫实例：唐诗三百首</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python爬虫进阶/">Python爬虫进阶</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python初步使用scrapy/">Python初步使用Scrapy</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python开发简单爬虫之实战演练/">Python开发简单爬虫之实战演练</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Python/Python开发简单爬虫之爬虫介绍（一）/">Python开发简单爬虫之爬虫介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch压力测试/">FreeSwitch压力测试</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch使用mod-xml-curl提供动态用户管理/">FreeSwitch使用mod_xml_curl提供动态用户管理</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/Tools/Win10下MarkdownPad安装及问题/">Win10下MarkdownPad安装及问题</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu14-04安装Apache-php5-mysql-phpmyadmin/">Ubuntu14.04安装Apache+php5+mysql+phpmyadmin</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/C/Linux下C语言入门准备/">Linux下C语言入门准备</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/vi编辑器/">vi编辑器</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell简单介绍/">Shell简单介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/linux基础（四）/">linux基础（四）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（三）/">Linux基础（三）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（二）/">Linux基础（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu Server 14.04 静态IP简单配置/">Ubuntu Server 14.04 静态IP简单配置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux基础（一）/">Linux基础（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Handler详解（一）-关联到UI线程/">Android之Handler详解（一）---关联到UI线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Handler详解（二）-关联到非UI线程/">Android之Handler详解（二）---关联到非UI线程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之更新UI的方法/">Android之更新UI的方法</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之延迟执行某个任务/">Android之延迟执行某个任务</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/AndroidStudio之环境设置 /">初次安装AndroidStudio之环境设置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/java里如何正确计算检验和/">java里如何正确计算检验和</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之AsyncTask介绍/">Android之AsyncTask介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（一）-简介/">Android之Fragment（一）--简介</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（二）-使用/">Android之Fragment（二）--使用</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（三）-生命周期与回退栈/">Android之Fragment（三）--生命周期与回退栈</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java运行时多态性：继承和接口的实现/">Java运行时多态性：继承和接口的实现</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之Fragment（四）-Fragment与Activity通讯/">Android之Fragment（四）--Fragment与Activity通讯</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之RAM、ROM和SD卡/">Android之RAM、ROM和SD卡</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之使用全局变量的两种方法/">Android之使用全局变量的两种方法</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-SharedPreference/">Android之数据存储--SharedPreference</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-File内部存储/">Android之数据存储--File内部存储</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存储-外部存储（SD卡）/">Android之数据存储--外部存储（SD卡）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之音频播放（MediaPlayer和SoundPool）/">Android之音频播放（MediaPlayer和SoundPool）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之话筒、听筒、扬声器/">Android之话筒、听筒、扬声器</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之数据存数-SQLite数据库/">Android之数据存数--SQLite数据库</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之AdapterView及子类/">Android之AdapterView及子类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之ListView和ListActivity-ArrayAdapter/">Android之ListView和ListActivity--ArrayAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之列表视图（LitView）-SimpleAdapter/">Android之列表视图（LitView）--SimpleAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之列表视图（LstView）-BaseAdapter/">Android之列表视图（LstView）--BaseAdapter</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之自动完成文本框（AutoCompleteTextView）/">Android之自动完成文本框（AutoCompleteTextView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之网格视图（GridView）/">Android之网格视图（GridView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之可扩展的列表组件（ExpandableListView）/">Android之可扩展的列表组件（ExpandableListView）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/ListView的性能优化之convertView和viewHolder/">ListView的性能优化之convertView和viewHolder</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/android-layout-weight的真实含义/">android:layout_weight的真实含义</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Android/Android之使用9Patch图片作为背景/">Android之使用9Patch图片作为背景</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java综述/">Java综述</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之数据类型、变量和数组/">Java之数据类型、变量和数组</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之运算符/">Java之运算符</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之控制语句/">Java之控制语句</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之类/">Java之类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之方法和类的深入分析/">Java之方法和类的深入分析</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之继承/">Java之继承</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之包和接口/">Java之包和接口</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之异常处理/">Java之异常处理</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之多线程编程/">Java之多线程编程</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之枚举、自动装箱与注解（元数据）/">Java之枚举、自动装箱与注解（元数据）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之I-O以及其它主题/">Java之I/O以及其它主题</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之lambda表达式/">Java之lambda表达式</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之字符串处理/">Java之字符串处理</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Java/Java之集合框架/">Java之集合框架</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala访问控制修饰符/">Scala访问控制修饰符</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala类/">Scala类</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala函数（一）/">Scala函数（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala函数（二）/">Scala函数（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Scala/Scala单例对象/">Scala单例对象</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——信令篇/">使用WebRTC搭建前端视频聊天室——信令篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——数据通道篇/">使用WebRTC搭建前端视频聊天室——数据通道篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——点对点通信篇/">使用WebRTC搭建前端视频聊天室——点对点通信篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/WebRTC工作流程/">WebRTC工作流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC实现/">Android之WebRTC实现</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC介绍（二）/">Android之WebRTC介绍（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/Android之WebRTC介绍（一）/">Android之WebRTC介绍（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/WebRTC之turn服务器搭建/">WebRTC之turn服务器搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/openfire之SSL认证/">openfire之SSL认证</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/基于openfire-smack开发Android即时聊天应用-一-用户注册、登陆、修改密码、注销等/">基于openfire+smack开发Android即时聊天应用[一]-用户注册、登陆、修改密码、注销等</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/ubuntu下opensips安装配置/">ubuntu下opensips安装配置</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/计算机是如何启动的？/">计算机是如何启动的？</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Linux的启动流程/">Linux的启动流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/XMPP/Smack Message属性扩展---添加自定义元素(标签)/">Smack Message属性扩展--添加自定义元素（标签）</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/DataStructure/线性表-顺序表/">线性表---顺序表</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP对话流程/">SIP对话流程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP服务器类型/">SIP服务器类型</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP注册过程/">SIP注册过程</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/openSIPS路由类型/">openSIPS路由类型</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips函数/">opensips函数</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/SIP/SIP路由字段和机理/">SIP路由字段和机理</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips-csipsimple出现的各种问题/">opensips+csipsimple出现的各种问题</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/OpenSIPs/opensips介绍/">opensips介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（八）-输入输出重定向、文件包含/">Shell编程（八）---输入输出重定向、文件包含</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（七）-函数/">Shell编程（七）---函数</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（六）-循环/">Shell编程（六）---循环</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（五）-if、case/">Shell编程（五）---if、case</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（四）-echo、printf/">Shell编程（四）---echo、printf</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（三）-字符串、数组/">Shell编程（三）---字符串、数组</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（二）-替换、运算符、注释/">Shell编程（二）---替换、运算符、注释</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Shell/Shell编程（一）/">Shell编程（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/WebRTC/使用WebRTC搭建前端视频聊天室——入门篇/">使用WebRTC搭建前端视频聊天室——入门篇</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/更改Yelee主题标签云为球形标签云/">更改Yelee主题标签云为球形标签云</a></li><li class="post-list-item"><a class="post-list-link" href="/Communication/FreeSwitch/FreeSwitch安装文档/">FreeSwitch安装文档</a></li><li class="post-list-item"><a class="post-list-link" href="/Comprehensive/BuildBlog/Hexo主题Yelee介绍/">Hexo主题Yelee介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/Programming/Linux/Ubuntu14-04安装JDK与配置环境变量/">Ubuntu14.04安装JDK与配置环境变量</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2017 FreeShow
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
    <script src="/js/GithubRepoWidget.js"></script>

<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
             github: ".github-widget a", 
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>